package com.chen.manager.service.impl;

import com.chen.manager.mapper.ProductDetailsMapper;
import com.chen.manager.mapper.ProductMapper;
import com.chen.manager.mapper.ProductSkuMapper;
import com.chen.manager.service.ProductService;
import com.chen.model.dto.product.ProductDto;
import com.chen.model.entity.base.ProductUnit;
import com.chen.model.entity.product.Product;
import com.chen.model.entity.product.ProductDetails;
import com.chen.model.entity.product.ProductSku;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.ognl.OgnlRuntime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author cgj
 * @date 3/12/25 7:27 PM
 */
@Transactional(rollbackFor = Exception.class)
@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductSkuMapper productSkuMapper;
    @Autowired
    private ProductDetailsMapper productDetailsMapper;

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Product> getByPage(Integer pageNum, Integer pageSize, ProductDto productDto) {
        //开启分页
        PageHelper.startPage(pageNum, pageSize);
        List<Product> productList = productMapper.selectByPage(productDto);
        return new PageInfo<>(productList);
    }

    /**
     * 保存商品
     * 涉及到三张表
     * 主表 product
     * 子表 product_sku  ,product_details
     * 涉及到几张表就有几个mapper
     * transactional 默认只处理运行时异常 runtimeException
     * 但是sqlException 和IOException不在runtimeException体系下
     * 解决 扩大 处理范围到 Exception
     *
     * @param product
     */
    @Override
    //扩大异常的处理范围
    @Transactional(rollbackFor = Exception.class)
    public void save(Product product) {
        // product_sku  ,product_details  需要用到 product 保存后产生的id,所有要先插入product
        //对前端没有传过来的数据进行补充
        product.setAuditStatus(0);    //审核状态
        product.setStatus(0);  //默认状态
        productMapper.insertProduct(product);

        // 保存product_sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        Integer index = 0;
        for (ProductSku productSku : productSkuList) {
            //设置product_id
            productSku.setProductId(product.getId());
//          sku_code 格式  1_0   设置product_id_index
            productSku.setSkuCode(String.valueOf(product.getId() + index));
            index++;
            //设置status
            productSku.setStatus(0);
            //设置sale_num
            productSku.setSaleNum(0);
            productSkuMapper.insertProductSku(productSku);
        }
        // 保存product_detail
        ProductDetails productDetails = new ProductDetails();
        productDetails.setProductId(product.getId());
        productDetailsMapper.insertProductDetail(productDetails);


    }

    /**
     * 根据id 查询商品详情
     * 数据来源三张表  主表product
     * 子表      product_details      product_sku
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(readOnly = true)
    public Product getById(Long id) {
        //查询商品表
        Product product = productMapper.selectById(id);
        //查询  product_sku表
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        product.setProductSkuList(productSkuList);
        ProductDetails productDetails = productDetailsMapper.selectByProductId(id);
        product.setDetailsImageUrls(productDetails.getImageUrls());
        return product;
    }

    /**
     * 需改
     * 主表   product  id
     * 子表   product_details product_sku   product_id
     *
     * @param product
     */
    @Override
    public void update(Product product) {
        //修改主表
        productMapper.updateProduct(product);
        //修改 product_sku
        List<ProductSku> productSkuList = product.getProductSkuList();
        // 为空直接跳过
        if (!CollectionUtils.isEmpty(productSkuList)) {
            for (ProductSku productSku : productSkuList) {
                productSkuMapper.updateProductSkuById(productSku);
            }
        }

        //修改product_details
        // 没有唯一的 id 需要先查询 product_details 的id
        ProductDetails productDetails = productDetailsMapper.selectByProductId(product.getId());
        productDetails.setImageUrls(product.getDetailsImageUrls());
        productDetailsMapper.updateProductDetail(productDetails);
    }

    /**
     * 根据id删除
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        // 删除子表product_details
        productDetailsMapper.deleteByProductId(id);
        //删除子表product_sku
        productSkuMapper.deleteByProductId(id);
        //删除主表  product
        productMapper.deleteById(id);
    }

    /**
     * 商品审核
     * @param id
     * @param auditStatus
     */
    @Override
    public void updateAuditStatus(Long id, Integer auditStatus) {
        String auditMessage="审核不通过";
        if(auditStatus==1){
            auditMessage="审核通过";
        }
        productMapper.updateAuditStatus(id,auditStatus,auditMessage);
    }

    /**
     * 商品的上下架  -1表示下架
     * 需要对两个表进行处理  product ,product_sku
     * 需要对两个表进行处理  product ,product_sku
     * @param id
     * @param status
     */
    @Override
    public void updateStatus(Long id, Integer status) {
        //对 product 处理   先查询在修改服用之前的接口
        Product product = productMapper.selectById(id);
        product.setStatus(status);
        productMapper.updateProduct(product);
        // 处理product_sku
        // 先拿到 product_sku的id
        List<ProductSku> productSkuList = productSkuMapper.selectByProductId(id);
        for (ProductSku productSku : productSkuList) {
            productSku.setStatus(status);
            productSkuMapper.updateProductSkuById(productSku);
        }


    }


}
